import React from 'react';
import { createForm } from 'rc-form';
import { Button, InputItem, WingBlank, WhiteSpace,Toast,List } from 'antd-mobile';
import 'antd-mobile/dist/antd-mobile.css';
import request from '../../../../utils/request';
import { hashHistory} from 'react-router';

let userMsg = {}
class UpdatePwd extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            pickerValue: [],
            dpValue: null,
        };
    }
    componentDidMount() {
        this.props.changeTitle('修改密码');
    }
    componentWillMount(){
        userMsg = this.props.location.state
    }

    //修改密码
    submit = (e) => {
        let fieldValue = this.props.form.getFieldsValue(); //{oldPwd: "11", newPwd: "11", confirmNewPwd: "11"}
        //得到旧密码
        let oldPwd = userMsg.password;

        if (fieldValue.oldPwd !== oldPwd){
            Toast.info("旧密码错误！",1);
            return;
        }
        if (fieldValue.newPwd == ''){
            Toast.info("新密码不能为空！",1);
            return;
        }
        if (fieldValue.newPwd !== fieldValue.confirmNewPwd ){
            Toast.info("两次输入的密码不一致！",1);
            return;
        }
        this.props.form.validateFields({ force: true },
            (err, values) => {
                if (!err) {
                    values = {...values,userId:userMsg.user_id};
                    // values.userId = userMsg.user_id;
                    request('PwdManagerService','updatePwd', {
                        method: 'POST',
                        body: values
                    }).then(function (value) {
                        if (value.data.result === 0){
                            Toast.info("修改成功！",1);
                            userMsg.password = values.newPwd;
                            hashHistory.push({
                                pathname: '/user/security',
                                state:userMsg
                            })
                        }else if (value.data.result === 1){
                            Toast.info("修改失败，请稍后再试！",1);
                        }
                    });
                }
            }
        );
    }

    render() {
        const { getFieldProps } = this.props.form;
        const { getFieldError } = this.props.form;
        return (
            <div className="my-list">
                <WhiteSpace/>
                <InputItem
                    {...getFieldProps('oldPwd',
                        {
                            initialValue: '',
                            rules: [
                                {required: true, message: '旧密码不能为空!'},
                            ],
                        })}
                    error={!!getFieldError('oldPwd')}
                    onErrorClick={() => {
                        Toast.fail(getFieldError('oldPwd'), 1);
                    }}
                    type="password"
                    placeholder="请输入旧密码">
                    旧密码
                </InputItem>
                <InputItem
                    {...getFieldProps('newPwd',
                        {
                            initialValue: '',
                        })}
                    error={!!getFieldError('newPwd')}
                    onErrorClick={() => {
                        Toast.fail(getFieldError('newPwd'), 1);
                    }}
                    type="password"
                    placeholder="请输入新密码">
                    新密码
                </InputItem>
                <InputItem
                    {...getFieldProps('confirmNewPwd',
                        {
                            initialValue: '',
                        })}
                    error={!!getFieldError('confirmNewPwd')}
                    onErrorClick={() => {
                        Toast.fail(getFieldError('confirmNewPwd'), 1);
                    }}
                    type="password"
                    placeholder="请再次输入新密码">
                    确认密码
                </InputItem>
                <WingBlank>
                    <WhiteSpace/>
                    <Button type="warning" size="large" onClick={this.submit}>确 定</Button><WhiteSpace/>
                </WingBlank>
            </div>
        );
    }
}

export default createForm()(UpdatePwd);
